﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Semantics;
using Semantics.Base;
using Semantics.UI;
using AutoResponse.Dialogue;
namespace AutoResponse
{
    public partial class DialogueForm : Form
    {
        DialogueProcessor dialogue = new DialogueProcessor();

        public DialogueForm()
        {
            this.InitializeComponent();
        }


        GraphDifference DictionaryDifference;
        const string DifferenceFilename = "AutoResponse.dif";
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                NLP.Initialize();
                if (System.IO.File.Exists(DifferenceFilename))
                    DictionaryDifference = GraphDifference.Load(DifferenceFilename);
                else
                    DictionaryDifference = new GraphDifference();
                DictionaryDifference.Attach((Graph)NLP.Dictionary);
                this.dialogue.Memory.IntiateGraph();
                this.richTextBoxDialogue.Text = dialogue.Hello();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }
        }



        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (richTextBoxInput.Text != "")
                {
                    this.ProcessInput();
                }
                else
                {
                    MessageBox.Show("发送内容不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (System.Exception exp)
            {

                MessageBox.Show(this, exp.ToString());
            }
        }


        GraphForm form;
        private void button2_Click(object sender, EventArgs e)
        {
            if (form == null)
            {
                form = new GraphForm();
                form.init(this.dialogue.Memory);
                form.Show();
                form.Closed += new EventHandler(form_Closed);
            }
            else
            {
                form.BringToFront();
            }
        }

        void form_Closed(object sender, EventArgs e)
        {
            this.form = null;
        }

        void OnNewNode(Node node)
        {
            if (this.form != null)
            {
                this.form.OnNewNode(node);
            }
        }

        string inputStr;
        void ProcessInput()
        {
            inputStr = this.richTextBoxInput.Text.Replace("\n", "");
            this.richTextBoxInput.Text = "";
            this.richTextBoxDialogue.Text += "你说:\n  " + inputStr + "\n";
            this.richTextBoxDialogue.Text += this.ProcessInput(inputStr);
            this.listViewPropositions.Items.Add(inputStr);

        }

        ProcessResult result;
        string ProcessInput(string text)
        {
            result = this.dialogue.Response(text);
            string response = result.Response;
            this.semanticNodeControl1.Nodes.Clear();
            //if (result.ParseResults != null)
            //{
            //    for (int i = 0; i < result.ParseResults.Count; i++)
            //    {
            //        this.semanticNodeControl1.ShowSemanticNode(result.ParseResults[i], this.semanticNodeControl1.Nodes, 0);
            //    }
            //    if (result.ParseResults.Count > 1)
            //    {
            //        MessageBox.Show("请选择一条分析正确的语句", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    }
            //}
            if (result.Result != null)
            {
                this.semanticNodeControl1.ShowSemanticNode(result.Result, this.semanticNodeControl1.Nodes, 0);
            }
            return response;
        }

        private void semanticNodeControl1_DoubleClick(object sender, EventArgs e)
        {
            if (this.semanticNodeControl1.Nodes.Count > 1)
            {
                for (int i = 0; i < this.semanticNodeControl1.Nodes.Count; i++)
                {
                    if (this.semanticNodeControl1.Nodes[i].IsSelected == true)
                    {
                        //ISemantizer semantizer = new Semantics.IR.Semantizer(KnowledgeBase.Reference.KnowledgeSet);
                        //result.Graph = semantizer.Semantize(result.ParseResults[i]);
                        //this.dialogue.Merge(result.Graph);
                        //string name = this.dialogue.SearchName(inputStr);
                        //result.Response = this.dialogue.OnNewContact(name);
                    }
                }
            }
        }

        private void buttonShow_Click(object sender, EventArgs e)
        {
            try
            {
                NLP.ShowClassHierarchy();
            }
            catch (Exception exp)
            {
                MessageBox.Show("Parsing Exception:" + exp.Message);
            }
        }

        private void ToolStripMenuItemAddWords_Click(object sender, EventArgs e)
        {
            NLP.Dictionary.InsertWord("与时俱进", "变动");
        }

        private void ToolStripMenuItemSaveDifference_Click(object sender, EventArgs e)
        {
            DictionaryDifference.Save(DifferenceFilename);
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            richTextBoxInput.Text = comboBox1.SelectedItem.ToString();
        }


    }
}
